<template>
  <div class="login-container">
    <el-form ref="formRef" :model="form" :rules="rules" class="login-form">
      <div class="title-container">
        <h3 class="title">用户登录</h3>
      </div>
      <el-form-item prop="username">
        <el-input v-model="form.username">
          <template #prefix>
            <svg-icon class="svg-container" icon="user"/>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" :type="passwordType">
          <template #prefix>
            <svg-icon class="svg-container" icon="password"/>
          </template>
          <template #suffix>
            <svg-icon :icon="passwordType === 'password' ? 'eye' : 'eye-open'" style="cursor:pointer;" @click="changeType"/>
          </template>
        </el-input>
      </el-form-item>
      <el-button class="login-button" type="primary" @click="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import {ref} from 'vue'

import {login} from '@/api'
import {ElMessage} from "element-plus"
import {useAppStore} from '@/stores/app'

const store = useAppStore()

const form = ref({
  username: 'admin',
  password: '123456'
})


const rules = ref({
  username: [
    {
      required: true,
      message: 'Please input Activity name',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: 'Please input Activity name',
      trigger: 'blur'
    }
  ]
})

import {useRouter} from 'vue-router'
const router = useRouter()
const formRef = ref(null)
const handleLogin = async () => {
  const {data, meta} = await login(form.value)

  //登录成功，保存令牌状态
  if (meta.status === 200) {
    ElMessage.success(meta.msg)
    //保存令牌状态
    store.setAuthorization(data.token)
    //路由跳转 /home
    router.push("/")

  }

}

const passwordType = ref('password')
const changeType = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    .login-button {
      width: 100%;
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
</style>
